var narr = new Array();
var codelength = 0;
function draw() {
    for (k = 0; k < 1; k++) {
        for (i = 0; i < 10; i++) {
            if (i + (k * 10) < 10) {
                $('#n1').append('<li data-item=' + (i + (k * 10)) + '>' + i + '</li>');
            }
        }
    }
    for (k = 0; k < 2; k++) {
        for (i = 0; i < 10; i++) {
            if (i + (k * 10) < 15) {
                $('#n2').append('<li data-item=' + (i + (k * 10)) + '>' + i + '</li>');
            }
        }
    }
    for (k = 0; k < 2; k++) {
        for (i = 0; i < 10; i++) {
            if (i + (k * 10) < 20) {
                $('#n3').append('<li data-item=' + (i + (k * 10)) + '>' + i + '</li>');
            }
        }
    }
    for (k = 0; k < 3; k++) {
        for (i = 0; i < 10; i++) {
            if (i + (k * 10) < 26) {
                $('#n4').append('<li data-item=' + (i + (k * 10)) + '>' + i + '</li>');
            }
        }
    }
    for (k = 0; k < 3; k++) {
        for (i = 0; i < 10; i++) {
            if (i + (k * 10) < 30) {
                $('#n5').append('<li data-item=' + (i + (k * 10)) + '>' + i + '</li>');
            }
        }
    }
    for (k = 0; k < 4; k++) {
        for (i = 0; i < 10; i++) {
            if (i + (k * 10) < 34) {
                $('#n6').append('<li data-item=' + (i + (k * 10)) + '>' + i + '</li>');
            }
        }
    }
}


function place() {
    deg1 = 360 / 10;
    deg2 = 360 / 15;
    deg3 = 360 / 20;
    deg4 = 360 / 26;
    deg5 = 360 / 30;
    deg6 = 360 / 34;

    $('#n1 li').each(function (index) {
        if(index==0) $(this).addClass('active');
        $(this).css({transform: 'rotateZ(' + deg1 * index + 'deg) translateX(' + parseInt(30) + 'px)'});
    });
    $('#n2 li').each(function (index) {
        if(index==0) $(this).addClass('active');
        $(this).css({transform: 'rotateZ(' + deg2 * index + 'deg) translateX(' + parseInt(45) + 'px)'});
    });
    $('#n3 li').each(function (index) {
        if(index==0) $(this).addClass('active');
        $(this).css({transform: 'rotateZ(' + deg3 * index + 'deg) translateX(' + parseInt(60) + 'px)'});
    });
    $('#n4 li').each(function (index) {
        if(index==0) $(this).addClass('active');
        $(this).css({transform: 'rotateZ(' + deg4 * index + 'deg) translateX(' + parseInt(75) + 'px)'});
    });
    $('#n5 li').each(function (index) {
        if(index==0) $(this).addClass('active');
        $(this).css({transform: 'rotateZ(' + deg5 * index + 'deg) translateX(' + parseInt(90) + 'px)'});
    });
    $('#n6 li').each(function (index) {
        if(index==0) $(this).addClass('active');
        $(this).css({transform: 'rotateZ(' + deg6 * index + 'deg) translateX(' + parseInt(105) + 'px)'});
    });
}
//TIMER
function n1(ts, timer, code, callback) {
    var current = $('#n1 li.active').text();
    if ((ts-1) == timer || (ts == 0 && code == current)) {console.log(ts, timer, code, current);completeCallback(1, callback); return false;}
    deg = 360 / 10 * ($('#n1 li').length - ts);
    $('#n1 li').removeClass('active');
    $('#n1 li').eq(ts).addClass('active');
    $('#n1').css({transform: 'rotateZ(' + deg + 'deg)'});
    ts++;
    setTimeout(function () {
        n1(ts, timer, code, callback)
    }, ts*10);
}
function n2(ts, timer, code, callback) {
    var current = $('#n2 li.active').text();
    if ((ts-1) == timer || (ts == 0 && code == current)) {console.log(ts, timer, code, current);completeCallback(2, callback); return false;}
    deg = 360 / 15 * ($('#n2 li').length - ts);
    $('#n2 li').removeClass('active');
    $('#n2 li').eq(ts).addClass('active');
    $('#n2').css({transform: 'rotateZ(' + deg + 'deg)'});
    ts++;
    setTimeout(function () {
        n2(ts, timer, code, callback)
    }, ts*10);
}
function n3(ts, timer, code, callback) {
    var current = $('#n3 li.active').text();
    if ((ts-1) == timer || (ts == 0 && code == current)) {console.log(ts, timer, code, current);completeCallback(3, callback); return false;}
    deg = 360 / 20 * ($('#n3 li').length - ts);
    $('#n3 li').removeClass('active');
    $('#n3 li').eq(ts).addClass('active');
    $('#n3').css({transform: 'rotateZ(' + deg + 'deg)'});
    ts++;
    setTimeout(function () {
        n3(ts, timer, code, callback)
    }, ts*10);
}
function n4(ts, timer, code, callback) {
    var current = $('#n4 li.active').text();
    if ((ts-1) == timer || (ts == 0 && code == current)) {console.log(ts, timer, code, current);completeCallback(4, callback); return false;}
    deg = 360 / 26 * ts;
    $('#n4 li').removeClass('active');
    $('#n4 li').eq(ts).addClass('active');
    $('#n4').css({transform: 'rotateZ(-' + deg + 'deg)'});
    ts++;
    setTimeout(function () {
        n4(ts, timer, code, callback)
    }, ts*10);
}
function n5(ts, timer, code, callback) {
    var current = $('#n5 li.active').text();
    if ((ts-1) == timer || (ts == 0 && code == current)) {console.log(ts, timer, code, current);completeCallback(5, callback); return false;}
    deg = 360 / 30 * ts;
    $('#n5 li').removeClass('active');
    $('#n5 li').eq(ts).addClass('active');
    $('#n5').css({transform: 'rotateZ(-' + deg + 'deg)'});
    ts++;
    setTimeout(function () {
        n5(ts, timer, code, callback)
    }, ts*10);
}
function n6(ts, timer, code, callback) {
    var current = $('#n6 li.active').text();
    if ((ts-1) == timer || (ts == 0 && code == current)) {console.log(ts, timer, code, current);completeCallback(6, callback); return false;}
    deg = 360 / 34 * ($('#n6 li').length - ts);
    $('#n6 li').removeClass('active');
    $('#n6 li').eq(ts).addClass('active');
    $('#n6').css({transform: 'rotateZ(' + deg + 'deg)'});
    ts++;
    setTimeout(function () {
        n6(ts, timer, code, callback)
    }, ts*10);
}



//unlock
function unlock(vCode, callback) {
    var code_arr = vCode.split("");
    narr.length = 0;
    codelength = code_arr.length;
    setTimeout(function () {
        var number = parseInt(code_arr[0]);
        // number = number == NaN ? 0 : number;
        n1(0, number, code_arr[0], callback);
    }, 500);
    setTimeout(function () {
        var number = parseInt(code_arr[1]);
        if(number != 0 && number < 5) {
            var numberarr = [number, 10 + number];
            number = numberarr[Math.floor(Math.random() * numberarr.length)];
        }
        // number = number == NaN ? 0 : number;
        n2(0, number, code_arr[1], callback);
    }, 500);
    setTimeout(function () {
        var number = parseInt(code_arr[2]);
        if(number != 0) {
            var numberarr = [number, 10 + number];
            number = numberarr[Math.floor(Math.random() * numberarr.length)];
        }
        // number = number == NaN ? 0 : number;
        n3(0, number, code_arr[2], callback);
    }, 500);
    setTimeout(function () {
        var number = parseInt(code_arr[3]);
        if(number != 0) {
            var numberarr = [number, 10 + number];
            // if(number >= 6) {
            //     numberarr = [number, 10 + number];
            // }else {
            //     numberarr = [number, 10 + number, 20 + number];
            // }
            number = numberarr[Math.floor(Math.random() * numberarr.length)];
        }
        // number = number == NaN ? 0 : number;
        n4(0, number, code_arr[3], callback);
    }, 250);
    setTimeout(function () {
        var number = parseInt(code_arr[4]);
        if(number != 0) {
            var numberarr = [number, 10 + number];
            // numberarr = [number, 10 + number, 20 + number];
            number = numberarr[Math.floor(Math.random() * numberarr.length)];
        }
        // number = number == NaN ? 0 : number;
        n5(0, number, code_arr[4], callback);
    }, 250);
    setTimeout(function () {
        var number = parseInt(code_arr[5]);
        if(number != 0) {
            var numberarr = [number, 10 + number];
            // if(number >= 4) {
            //     numberarr = [number, 10 + number, 20 + number];
            // }else {
            //     numberarr = [number, 10 + number, 20 + number, 30 + number];
            // }
            number = numberarr[Math.floor(Math.random() * numberarr.length)];
        }
        // number = number == NaN ? 0 : number;
        n6(0, number, code_arr[5], callback);
    }, 0);

}

function completeCallback(n, callback) {
    narr.push(n);
    if(narr.length >= codelength) {
        console.log("结束")
        narr.length = 0;
        codelength = 0;
        if(typeof callback == "function") {
            callback("shield", "complete");
        }
    }
}

var boo=0;
var canUse=document.getElementsByTagName("body")[0].style;
if(typeof canUse.animation!="undefined"||typeof canUse.WebkitAnimation!="undefined"){
    boo=1;/*支持动画*/
}else{
    boo=0;/*不支持动画*/
}
/*obj,actionName,speed都是 string,time(秒)是int类型*/
function actionIn(obj,actionName,time,speed, callback){
    if(boo==1){
        $(obj).show();
        $(obj).css({"animation":actionName+" "+time+"s"+" "+speed,"animation-fill-mode":"forwards"});
    }else {
        $(obj).show();
    }
    setTimeout(function () {
        if(typeof callback == "function") {
            callback();
        }
    }, time * 1000);
}

/*obj,actionName,speed都是 string,time(秒)是int类型*/
function actionOut(obj,actionName,time,speed, callback){
    if(boo==1) {
        $(obj).css({"animation":actionName+" "+time+"s"+" "+speed});
        var setInt_obj=setInterval(function(){
            $(obj).hide();
            clearInterval(setInt_obj);
            if(typeof callback == "function") {
                callback();
            }
        },time*1000);
    }else {$(obj).hide();}
}

function playShield(vCode, callback) {
    if($("#shield ul li").length == 0) {
        draw();
        place();
    }
    $("#shield ul").each(function (index) {
        $('#shield ul').css({transform: 'rotateZ(' + 0 + 'deg)'});
        $("#shield ul:eq("+index+")").find("li").removeClass('active');
        $("#shield ul:eq("+index+")").find("li:first").addClass('active');
    });
    actionIn("#shield", 'action_scale', 1, "", function () {
        unlock(vCode, callback);
    });
}

function shieldSuccess(callback) {
    $('#shield::before').addClass('opacity', '1');
    $('#shield').addClass('success');
    setTimeout(function () {
        actionOut("#shield",'action_scaleOut',1,"", function () {
            if(typeof callback == "function") {
                callback();
            }
            $('#shield').removeClass('success');
        });
    }, 1500);
}

function shieldFail(callback) {
    $('#shield').addClass('fail');
    $('.vcode-right-shield').addClass('am-animation-shake');
    setTimeout(function () {
        $('#shield').removeClass('fail');
        $('.vcode-right-shield').removeClass('am-animation-shake');
        /*actionOut("#shield",'action_scaleOut',1,"", function () {
            if(typeof callback == "function") {
                callback();
            }
        });*/
    }, 1500);
}
